package com.pentajock.docdb.dialogs;

import com.pentajock.docdb.AdminWindow;
import com.pentajock.docdb.ViewMode;
import com.pentajock.docdb.database.DBController;
import com.pentajock.docdb.images.DBImages;
import com.pentajock.docdb.types.Document;
import com.pentajock.docdb.types.User;
import com.pentajock.docdb.utils.Utils;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

public class DocumentPropertiesDialog extends JDialog {
	
	private JLabel docLabel;
	private JComboBox results;
	private JButton okButton;
	private JButton bulbButton;
	private JButton acceptChangesButton;
	private JButton revertChangesButton;
	private JTextField propertyTextField;
	private User userAccount;
	private Document document;
	private Object[][] components;

	public DocumentPropertiesDialog(AdminWindow owner, User u, Object document) {
		super(owner, "Edit Document Properties", true);
		Utils.setIconImage(this, DBImages.GENERIC_ICON.getImage());
		if (document instanceof Document && document != null)
			this.document = (Document)document;
		else
			dispose();
		
		components = this.document.getComponents();
		checkUser(u);
		initializeObjects();
		layoutFrame();
		addListeners();
		results.setSelectedIndex(0);
		getRootPane().setDefaultButton(okButton);
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
		setLocationRelativeTo(owner);
	}
	
	private void initializeObjects() {
		Icon icon;
		try {
			icon = DBImages.getMimeType(document);
		}
		catch (Exception e) {
			icon = DBImages.GENERIC_ICON.getIcon();
		}
		docLabel = new JLabel("<HTML><B>" + document.getDocumentName() 
			+ "</B></HTML>", icon , SwingConstants.LEFT);
		
		results = new JComboBox(components[1]);
		propertyTextField = new JTextField();
		okButton = new JButton("Close");
		acceptChangesButton = new JButton("Save", DBImages.SAVE_BUTTON.getIcon());
		revertChangesButton = new JButton("Revert", DBImages.REVERT_BUTTON.getIcon());
		bulbButton = new JButton(DBImages.BULB_ICON.getIcon());
	}
	
	private void layoutFrame() {
		Insets tenPadding = new Insets(10,10,10,10);
		Insets fivePadding = new Insets(5,5,5,5);
		Border etched = BorderFactory.createEtchedBorder();
		
		int yes = GridBagConstraints.HORIZONTAL;
		int no = GridBagConstraints.NONE;
		
		acceptChangesButton.setBorder(etched);
		revertChangesButton.setBorder(etched);
		bulbButton.setBorder(etched);
		
		JPanel panel = new JPanel(); 
		panel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = no;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 4;
		c.insets = tenPadding;
		panel.add(docLabel, c);
		
		c.fill = yes;
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 4;
		c.insets = fivePadding;
		panel.add(propertyTextField, c);
		
		c.fill = no;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		panel.add(results, c);
		
		c.gridx = 1;
		panel.add(bulbButton, c);

		c.fill = no;
		c.gridx = 2;
		c.gridy = 2;
		c.gridwidth = 1;
		panel.add(acceptChangesButton, c);
		c.gridx = 3;
		panel.add(revertChangesButton, c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.fill = yes;
		panel.add(new JSeparator());
		
		c.fill = no;
		c.gridx = 3;
		c.gridy = 5;
		c.insets = tenPadding;
		
		panel.add(okButton, c);
		getContentPane().add(panel);
	}
	
	private void addListeners() {
		
		propertyTextField.getDocument().addDocumentListener(new DocumentListener() {
			int i = 0;
			public void insertUpdate(DocumentEvent e) {
				disableControls();	
			}
			public void removeUpdate(DocumentEvent e) {
				disableControls();		
			}
			public void changedUpdate(DocumentEvent e) {
			}
			private void disableControls() {
				if (results.getSelectedIndex() == 0) {
					acceptChangesButton.setEnabled(false);
					revertChangesButton.setEnabled(false);
					propertyTextField.setEnabled(false);
					bulbButton.setEnabled(false);
					return;
				}
				else
					propertyTextField.setEnabled(true);
					
				if (!acceptChangesButton.isEnabled())
					i++;
				if (i > 0) {
					acceptChangesButton.setEnabled(true);
					revertChangesButton.setEnabled(true);
					i = 0;
				}
			}
		});
		
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		bulbButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = valueHelperDialog();
				if (s != null)
					propertyTextField.setText(s);
			}
		});
		
		results.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = results.getSelectedIndex(); 
				Object o = components[0][index];
				propertyTextField.setText(o==null?"":o.toString());
				acceptChangesButton.setEnabled(false);
				revertChangesButton.setEnabled(false);
				bulbButton.setEnabled(true);
			}
		});
		
		revertChangesButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				results.setSelectedIndex(results.getSelectedIndex()); 
			}
		});
	}
	
	private String valueHelperDialog() {
		String s = null;
		java.util.List<?> list = null;
		try {
			switch (results.getSelectedIndex()) {
				case 0:break;
				case 1: list = DBController.getAllDepartments();
					ViewMode.listMode = ViewMode.DEPARTMENT_MODE;break;
				case 2:break;
				case 3: list = DBController.getExtensions(); 
					ViewMode.listMode = ViewMode.EXTENSION_MODE;break;
				case 4: list = DBController.getCategories(); 
					ViewMode.listMode = ViewMode.CATEGORY_MODE;break;
				case 5:break;
				case 6:break;
				case 7: String fcPath = Utils.getDocRoot() + propertyTextField.getText();
					JFileChooser fc = new JFileChooser(fcPath);
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					fc.showOpenDialog(this);
					s = fc.getSelectedFile().getPath().substring(Utils.getDocRoot().length());
					break;
				case 8: s = Dialogs.showConfirm(document, "Set approved?").toString(); break;
				case 9:break;
				case 10: list = DBController.getUsers(userAccount.getDepartmentName()); break;
				default:
			}
		}
		catch (SQLException sql) {
			Dialogs.showError("SQL error retreiving some items");
		}
		if (list != null) {
			Object o = Dialogs.showListDialog(this, list);
			if (o instanceof User)
				s = ((User)o).getUserName();
			else if (o instanceof String)
				s = (String)o;
		}
		
		return s;

	}
	
	private void checkUser(User u) {
		if (!DBController.getUserIsAdmin(u))
			dispose();
		else
			userAccount = u;
	}
}
